﻿var ns, toast, actionsheet, UIAlbumBrowser, superFile, fileBrowser;
var app = new Vue({
    el: '#app',
    data: {
        chooseType: 1,
        show: false,
        writeCount: 0,//输入的文字数量
        examine_writenum: 0,//审批原因字数
        wenjianlist: [],
        outTypemsg: [],
        file: {
            ID: 0,
            CredentialName: '',//文件名字
            CredentialUrl: '',//文件路径
        },
        tdTitle:'',//用印标题
        tdRemark:'',//用印原因
        Number: 0,//用印次数
        VisitType:0,//用印类型ID
        VisitTypeName:'',//用印类型
        Remark:'',//申请说明
        ApprovalUserID: [],//审核人
        SendUserIDs: [],//抄送人
        fileList:[],//文件id
        select_id_list: [],//选中的抄送人员(多选)
        choose_id_list: [],//选中的审批人员(单选)
        fileID: 1,
        
    },
    methods: {
        // 选择用印类型
        do_choose_type: function () {
            var that = this;
            that.show = true;
        },
        get_outTypemsg: function () {
            var that = this;
            var options = {};
            options.action = 'loadtsealbasic';
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        that.outTypemsg = data.yongying_list;
                        that.VisitType = data.yongying_list[0].ID;
                        that.VisitTypeName = data.yongying_list[0].Name;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        // 选择用印类型
        doSelectRoom: function (item) {
            var that = this;
            that.show = false;
            that.VisitType = item.ID;
            that.VisitTypeName = item.Name;
        },
        //用印原因限制字数
        writeMsg: function () {
            var that = this;
            var testVal = that.tdRemark.length;
            that.writeCount = testVal;

        },
        //审批申请限制字数
        examine_writeMsg: function () {
            var that = this;
            that.examine_writenum = that.Remark.length;
        },
        //上传文件
        add_wenjian: function () {
            var that = this;

            fileBrowser.open({
                confirm: true
            }, function (ret) {
                fileBrowser.close();
                // alert(JSON.stringify(ret))
                // alert(JSON.stringify(ret && ret.status))
                if (ret && ret.status) {
                    const url = ret.url || ""
                    // var i = url.lastIndexOf('/');
                    // var j = url.length;
                    // that.form.wenjianName = url.slice(i + 1, j);
                    // that.form.wenjianUrl = url;
                    // alert(JSON.stringify(that.form.wenjianUrl));
                    // that.wenjianlist.push({ id: that.fileID, name: that.form.wenjianName, url: that.form.wenjianUrl });
                    // that.fileID++
                    // alert(JSON.stringify(that.wenjianlist));
                    // 多规格图片上传
                    const params = {
                        action: "fileupload",
                    }
                    // alert("url----"+JSON.stringify(url))
                    ns.post(params, function (succeed, data, err) {
                        if (succeed) {
                            that.file.ID = data.imgs[0].ID;
                            that.file.CredentialName = data.imgs[0].FileName;
                            that.file.CredentialUrl = data.imgs[0].FilePath;
                            that.wenjianlist.push(data.imgs[0]);
                            // that.fileList.push(data.imgs[0].ID);
                            // alert(JSON.stringify(that.fileList))
                            
                        }
                    }, {
                        files: {
                            file: [url]
                        }
                    })
                    return;
                }
                return false;
            });
        },
        open_flie: function () {
            var that = this;
            api.download({
                // 
                url: that.file.CredentialUrl,
                savePath: 'fs://app/' + that.file.CredentialName,
                report: true,
                cache: true,
                allowResume: true
            }, function (ret, err) {
                if (ret.state == 1) {
                    if (api.systemType == 'ios') {
                        superFile.open({
                            path: ret.savePath,
                        })

                    }
                    if (api.systemType == "android") {
                        superFile.open({
                            path:ret.savePath,
                        })
                    }
                }
            })
        },
        //删除文件
        do_delete: function (id) {
            var that=this;
            var options={};
            options.ID=id;
            options.ModifyType=0;
            options.action="delfile"
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    if(data.status){
                        that.wenjianlist = that.wenjianlist.filter((item) => {
                            return item.ID != id
                        });
                    }

                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //提交
        do_save: function () {
            var that = this;
            var SendUserIDs = [];

            if (that.VisitTypeName == "") {
                api.toast({
                    msg: '请选择用印类型',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            if (that.Number == 0) {
                api.toast({
                    msg: '请填写用印次数',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.tdTitle=='') {
                api.toast({
                    msg: '请填写用印标题',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.tdRemark=='') {
                api.toast({
                    msg: '请填写用印原因',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.choose_id_list.length <= 0) {
                api.toast({
                    msg: '请选择审批人',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            for (var i = 0; i < that.wenjianlist.length; i++) {
                var item = that.wenjianlist[i].ID;
                that.fileList.push(item);
            }
            for (var i = 0; i < that.choose_id_list.length; i++) {
                var item = that.choose_id_list[i].UserID;
                that.ApprovalUserID.push(item);
            }

            for (var i = 0; i < that.select_id_list.length; i++) {
                var item = that.select_id_list[i].UserID;
                SendUserIDs.push(item);
            }
            that.SendUserIDs = JSON.stringify(SendUserIDs);

            var options={};
            options.ApprovalUserID=JSON.stringify(that.ApprovalUserID);
            options.SendUserIDs=that.SendUserIDs;
            options.Type=1;
            options.Number=that.Number;
            options.tdSealBasics=that.VisitType;
            options.tdTitle=that.tdTitle;
            options.tdRemark=that.tdRemark;
            options.Remark=that.Remark;
            options.fileList=JSON.stringify(that.fileList);
            options.action='savasealapplication';
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'purchase_updata',
                    });
                    api.sendEvent({
                        name:'reloadexaminelist'
                    })
                    setTimeout(() => {
                        api.closeWin();
                    }, 400);

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //展开选择的全部人员
        open_more: function (type) {
            var that = this;
            //审批全部
            if (type == 1) {
                ns.openWin('examine_Allperson', '全部审核人员', {
                    list: that.choose_id_list,
                    type: type,
                });
            } else if (type == 0) {
                ns.openWin('examine_Allperson', '全部抄送人员', {
                    list: that.select_id_list,
                    type: type,
                });
            }

        },

        //选择人
        choose_persons: function (IsApproval) {
            var that = this;
            that.chooseType = IsApproval;
            // IsApproval==0//抄送
            if (IsApproval == 0) {
                // ns.openWin('chooseperson_frm', '请选择', {
                //     source: 'transfer',
                // });
                ns.openWin('chooseuser_frm', '选择抄送人', {
                    source: 'transfer',
                }); 
            }
            //IsApproval==1//审批
            if (IsApproval == 1) {
                // ns.openWin('chooseperson_frm', '请选择', {
                //     source: 'transfer',
                // });
                ns.openWin('chooseuser_frm', '选择审批人', {
                    source: 'transfer',
                }); 
            }

        },
        //移除抄送人
        guanbi_pic: function (id, IsApproval) {
            var that = this;
            that.chooseType = IsApproval;
            if (that.chooseType == 1) {
                that.choose_id_list = that.choose_id_list.filter((item) => {
                    return item.UserID != id
                });
            }
            if (that.chooseType == 0) {
                that.select_id_list = that.select_id_list.filter((item) => {
                    return item.UserID != id
                });
            }

        },
        // 抄送人
        select_person: function (select_list, type) {
            var that = this;
            if (type == 0) {
                for (var i = 0; i < select_list.length; i++) {
                    var item = select_list[i];
                    that.select_id_list.push(item);
                }
            } else {
                for (var i = 0; i < select_list.length; i++) {
                    var item = select_list[i];
                    that.choose_id_list.push(item);
                }
            }

            let obj = {};
            if (type == 0) {
                let peon = that.select_id_list.reduce((cur, next) => {
                    obj[next.UserID] ? "" : obj[next.UserID] = true && cur.push(next);
                    return cur;
                }, []);
                that.select_id_list = peon;
            } else {
                let peon = that.choose_id_list.reduce((cur, next) => {
                    obj[next.UserID] ? "" : obj[next.UserID] = true && cur.push(next);
                    return cur;
                }, []);
                that.choose_id_list = peon;
            }

        }


    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    // UIAlbumBrowser = api.require('UIAlbumBrowser');
    fileBrowser = api.require('fileBrowser');
    superFile = api.require('superFile');
    // bmLocation = api.require('bmLocation');
    app.get_outTypemsg();
    api.addEventListener({
        name: 'purchase_add'
    }, function (ret) {
        if (app.chooseType == 1) {
            var chooselist = ret.value.data;
            app.select_person(chooselist, 1);
            // app.choose_id_list=ret.value.data;
        }
        if (app.chooseType == 0) {

            var select_list = ret.value.data;
            app.select_person(select_list, 0);
        }

    })

    api.addEventListener({
        name: 'purchase_update'
    }, function (ret) {
        if (ret.value.type == 1) {
            app.choose_id_list = ret.value.data;
        }
        if (ret.value.type == 0) {
            app.select_id_list = ret.value.data;
        }

    })

    api.addEventListener({
        name: 'do_open_neibubaoshi'
    }, function (ret, err) {
        ns.openWin('purchase_list', '申请记录');
    });

};
